package com.tarena.lbs.user.dao.repository.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.pager.BasePageQuery;
import com.tarena.lbs.user.dao.mapper.AdminMapper;
import com.tarena.lbs.user.dao.mapper.UserMapper;
import com.tarena.lbs.user.dao.repository.AdminRepository;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.po.UserPO;
import com.tarena.lbs.user.pojo.query.AdminQuery;
import com.tarena.lbs.user.pojo.query.UserListQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.stereotype.Repository;


import java.util.List;

@Repository
public class AdminRepositoryImpl implements AdminRepository {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private UserMapper userMapper;



    @Override
    public AdminPO getAdminByPhone(String phone) {
        QueryWrapper<AdminPO> query = new QueryWrapper<>();
        query.eq("account_phone",phone);
        return adminMapper.selectOne(query);
    }

    @Override
    public AdminPO getAdminById(Integer id) {
        return adminMapper.selectById(id);
    }

    @Override
    public PageInfo<AdminPO> pagesList(AdminQuery query) {
        QueryWrapper<AdminPO> queryWrapper = assembleAdminQuery(query);
        //PageHelper.startPage 之间不能有代码
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<AdminPO> pos = adminMapper.selectList(queryWrapper);
        //selectList 数据库操作 之间不能有代码
        return new PageInfo<>(pos);
    }

    @Override
    public void insertSelective(AdminPO po) {
        adminMapper.insert(po);
    }

    @Override
    public AdminPO getAdminByAccountPhone(String accountPhone) {
        QueryWrapper<AdminPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_phone",accountPhone);
        return adminMapper.selectOne(queryWrapper);
    }

     @Override
    public PageInfo<UserPO> userPagesList(UserListQuery query) {
        //1.用户名 模糊查询 非空 where user_name like(%#{}%)
        QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(query.getUserName())){
            queryWrapper.like("user_name",query.getUserName());
        }
        //2.手机号 相等查询 where phone=#{phone}
        if (StringUtils.isNotBlank(query.getPhone())){
            queryWrapper.eq("phone",query.getPhone());
        }
        //起始时间查询
         if (query.getStartTime()!=null && query.getEndTime()!=null){
            queryWrapper.ge("reg_time",query.getStartTime());
             queryWrapper.le("reg_time",query.getEndTime());
        }
        //3.分页查询
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<UserPO> pos = userMapper.selectList(queryWrapper);
        return new PageInfo<>(pos);
    }

    @Override
    public void deleteById(Integer userId) {
        adminMapper.deleteById(userId);
    }

    @Override
    public void updateById(AdminPO adminPO) {
        adminMapper.updateById(adminPO);
    }

    @Override
    public void resetPassword(Integer id) {
        UpdateWrapper<AdminPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("account_password", "123456");
        updateWrapper.eq("id", id);
        adminMapper.update(null, updateWrapper);
    }

    private QueryWrapper<AdminPO> assembleAdminQuery(AdminQuery query) {
        QueryWrapper<AdminPO> queryWrapper = new QueryWrapper<>();
        //1.accountPhone 账号手机号 相等查询 where account_phone=#{phone}
        if (StringUtils.isNotBlank(query.getAccountPhone())){
            queryWrapper.eq("account_phone",query.getAccountPhone());
        }
        //2.nickname 昵称 模糊查询 非空 where nickname like(%#{}%)
        if (StringUtils.isNotBlank(query.getNickname())){
            queryWrapper.like("nickname",query.getNickname());
        }
        return queryWrapper;
    }
}
